#include <queue>
#include <vector>
using namespace std;
#include <boost/shared_ptr.hpp>
#include <wx/wx.h>
#include <wx/thread.h>
#include <wx/string.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/listctrl.h>
#include "ItemInfo.h"
#include "SyncQueue.h"
#include "FilePropertiesWatcher.h"
#include "BackgroundIconLoader.h"
#include "Utils.h"

BackgroundIconLoader::BackgroundIconLoader() : done(false)
{

}
void BackgroundIconLoader::RequestIcon(const wxString &fullPath, bool isDirectory)
{
    queue.Enqueue(make_pair(fullPath, isDirectory));
}
void BackgroundIconLoader::RegisterFilePropertiesWatcher(FilePropertiesWatcher *filePropertiesWatcher)
{
    filePropertiesWatchers.push_back(filePropertiesWatcher);
}
void BackgroundIconLoader::Initialize()
{
    Create();
    Run();
}
void BackgroundIconLoader::End()
{
    done = true;
    RequestIcon("", true);
}
wxThread::ExitCode BackgroundIconLoader::Entry()
{
    while (!TestDestroy())
    {
        if (done)
            continue;
        pair<wxString, bool> file = queue.Dequeue();
        if (!file.first.empty())
        {
            FileProperties fp;
            fp.changed = FILE_PROPERTY_ICON;
            fp.icon = GetIconFromFile(file.first, file.second);
            for (unsigned int i = 0; i < filePropertiesWatchers.size(); i++)
                filePropertiesWatchers[i]->FilePropertyChanged(file.first, fp);
        }
    }
    return 0;
}